//
//  GameViewController.swift
//  DYZB
//
//  Created by 1 on 16/10/8.
//  Copyright © 2016年 小码哥. All rights reserved.
//

import UIKit

private let kEdgeMargin : CGFloat = 10
private let kItemW : CGFloat = (kScreenW - 2 * kEdgeMargin) / 3
private let kItemH : CGFloat = kItemW * 6 / 5
private let kHeaderViewH : CGFloat = 50
private let KGameViewH : CGFloat = 90

private let kGameCellID = "kGameCellID"
private let kHeaderViewID = "kHeaderViewID"

class GameViewController: ZWLAnimViewController {
    
    // MARK: 懒加载属性
    fileprivate lazy var RemVM : ZWLHomeVM = ZWLHomeVM()
    fileprivate lazy var gameVM : ZWLGameVcViewModel = ZWLGameVcViewModel()
    fileprivate lazy var collectionView : UICollectionView = {[unowned self] in
        // 1.创建布局
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kItemW, height: kItemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 0, left: kEdgeMargin, bottom: 0, right: kEdgeMargin)
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
        
    
        
        // 2.创建UICollectionView
        let y = KpagetitleH
        let rect = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH - kStatusBarH - kNavigationBarH - KpagetitleH - kTabbarH)
        let collectionView = UICollectionView(frame: rect, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.register(UINib(nibName: "CollectionGameCellTwo", bundle: nil), forCellWithReuseIdentifier: kGameCellID)
        collectionView.register(UINib(nibName: "ZWLHeaderCollectionViewCell", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
        collectionView.dataSource = self
        collectionView.contentInset = UIEdgeInsets(top: (KGameViewH + kHeaderViewH), left: 0, bottom: 0, right: 0)
        return collectionView
    }()
    
    lazy var headerView : ZWLHeaderCollectionViewCell = {
        let headerView = ZWLHeaderCollectionViewCell.headerCollectionViewCell()
        headerView.frame = CGRect(x: 0, y: -(kHeaderViewH + KGameViewH), width: kScreenW, height: kHeaderViewH)
        headerView.preLabel.text = "常用"
        headerView.iconView.image = UIImage(named: "Img_orange")
        headerView.morebtn.isHidden = true
        return headerView
    }()
    
    lazy var gameView : ZWLGameView = {
        let gameView = ZWLGameView.gameView()
        gameView.backgroundColor = UIColor.red
        gameView.frame = CGRect(x: 0, y: -KGameViewH, width: kScreenW, height: KGameViewH)
        return gameView
    }()
    
    // MARK: 系统回调
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        loadData()
    }

}


// MARK:- 设置UI界面
extension GameViewController {
    internal override func setupUI() {
        
        contentView = collectionView
        view.addSubview(collectionView)
        collectionView.addSubview(headerView)
        collectionView.addSubview(gameView)
        super.setupUI()
    }
}


// MARK:- 请求数据
extension GameViewController {
    fileprivate func loadData() {
        gameVM.loadAllGameData {
            self.collectionView.reloadData()
            self.loadDataFinished()
            
        }
        
        RemVM.requestData {
            self.gameView.groups = self.RemVM.anchorGroups
        }
        
        
    }
}


// MARK:- 遵守UICollectionView的数据源&代理
extension GameViewController : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return gameVM.games.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // 1.获取cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGameCellID, for: indexPath) as! CollectionGameCellTwo
        
        cell.baseGame = gameVM.games[indexPath.item]
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        // 1.取出HeaderView
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath) as! ZWLHeaderCollectionViewCell
        
        // 2.给HeaderView设置属性
        headerView.preLabel.text = "全部"
        headerView.iconView.image = UIImage(named: "Img_orange")
        headerView.morebtn.isHidden = true
        
        return headerView
    }
}
